package com.twu.biblioteca.options;

import com.twu.biblioteca.Movie;
import com.twu.biblioteca.MovieRepository;
import com.twu.biblioteca.Option;
import com.twu.biblioteca.UserInterface;

/**
 * Created with IntelliJ IDEA.
 * User: aulme
 * Date: 05/07/2013
 * Time: 09:42
 * To change this template use File | Settings | File Templates.
 */
public class ViewMoviesInCollectionOption implements Option {
    private final UserInterface ui;
    private final MovieRepository movieRepository;

    public ViewMoviesInCollectionOption(UserInterface ui, MovieRepository movieRepository) {
        this.ui = ui;
        this.movieRepository = movieRepository;
    }

    @Override
    public void Execute() {
        this.ui.showMessage("Movies in collection:");
        printTableHeader();
        for (Movie movie : this.movieRepository.getAll()) { printMovie(movie); }
    }

    @Override
    public String toString() {
        return "View movies in collection";
    }

    private void printMovie(Movie movie) {
        printFormattedToTable(movie.getName(), String.valueOf(movie.getYear()), movie.getDirector(), movie.getFormattedRating());
    }

    private void printTableHeader() {
        printFormattedToTable("Movie", "Year", "Director", "Rating");
    }

    private void printFormattedToTable(String column1, String column2, String column3, String column4) {
        String formatted = String.format("%-57s%-12s%-23s%s", column1, column2, column3, column4);
        this.ui.showMessage(formatted);
    }
}
